from datetime import timezone
from datetime import timedelta

import requests
from bs4 import BeautifulSoup
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from util import *

source_id = 2

def get_utc8now():
    utcnow = datetime.now(timezone.utc)
    utc8now = utcnow.astimezone(timezone(timedelta(hours=8)))
    return utc8now

def crawl_baidu_top(board='realtime'):
    response = requests.get('https://top.baidu.com/board?tab={}'.format(board))
    soup = BeautifulSoup(response.text, 'html.parser')
    record_tags = soup.find_all('div', {'class': 'category-wrap_iQLoo'})
    titles, hot_indices = [], []
    for item in record_tags:
        title_tag = item.find('div', {'class': 'c-single-text-ellipsis'})
        hot_index_tag = item.find('div', {'class': 'hot-index_1Bl1a'})
        if (title_tag is not None) and (hot_index_tag is not None):
            titles.append(title_tag.text.strip())
            hot_indices.append(hot_index_tag.text.strip())
    return titles, hot_indices

def baidu_words():
    titles, hot_indices = crawl_baidu_top()
    records = list(zip(titles, hot_indices))
    hot_words = []
    if records and len(records) > 0:
        all_data_tuple = []
        params = ("id", "source_id", "title", "is_new","is_hot","is_fei","create_time", "hot_index", "category","onboard_time","info_complete")
        create_time = str(int(time.time()))
        for val in records:
            item = {
                "id": int(unique_id()),
                "source_id": source_id,
                "title": val[0],
                "is_new":0,
                "is_hot":0,
                "is_fei":0,
                "create_time": create_time,
                "hot_index": val[1],
                "category":"",
                "onboard_time":0,
                "info_complete": 0
            }
            hot_words.append(item)
            all_data_tuple.append(tuple(item.values()))
        flag = insert_hot_words(params, hot_words)
        if flag:
            return hot_words
    return []



if __name__ == '__main__':
    data_list = baidu_words()
    if type(data_list) == list and len(data_list) > 0:
        print('百度热词搜索结束')
        baidu_search(data_list)
    elif type(data_list) == list and len(data_list) == 0:
        print('百度热词搜索结果为空')
    else:
        print('百度搜索失败;未进行百度搜索')

